from sklearn.externals import joblib

import bowen
import xgboost as xgb
import pandas as pd

'''建立转发数的模型'''
#转发预测值
y_train = bowen.train1.loc[:, ['forward_count']]          #交叉验证
X_train = bowen.train1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

y_test = bowen.test1.loc[:,['forward_count']]
X_test = bowen.test1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

model_xgb = xgb.XGBRegressor(max_depth=4, colsample_btree=0.1, learning_rate=0.1, n_estimators=320, min_child_weight=2)
model_xgb.fit(X_train, y_train) #训练模型
joblib.dump(model_xgb,'''zf_model.m''') #保存模型
xgb_pred = model_xgb.predict(X_test)    #模型预测的转发数

# print(y_test)
xgb_pred_new=[]
for i in xgb_pred:
    if i<0:
        i=0
    i=int(i)
    xgb_pred_new.append(i)
xgb_pred_new=pd.DataFrame(xgb_pred_new)

'''建立评论数的模型'''
#评论预测值
y1_train = bowen.train1.loc[:, ['comment_count']]          #交叉验证
X1_train = bowen.train1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

y1_test = bowen.test1.loc[:,['comment_count']]
X1_test = bowen.test1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

model_xgb1 = xgb.XGBRegressor(max_depth=4, colsample_btree=0.1, learning_rate=0.1, n_estimators=320, min_child_weight=2)
model_xgb1.fit(X1_train, y1_train) #训练模型
joblib.dump(model_xgb1,'''pl_model.m''') #保存模型
xgb_pred1 = model_xgb1.predict(X1_test)
print(xgb_pred1)


xgb_pred_new1=[]
for i in xgb_pred1:
    if i<0:
        i=0
    i=int(i)
    xgb_pred_new1.append(i)
xgb_pred_new1=pd.DataFrame(xgb_pred_new1)

'''建立点赞数的模型'''
#赞预测值
y2_train = bowen.train1.loc[:, ['like_count']]          #交叉验证
X2_train = bowen.train1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

y2_test = bowen.test1.loc[:,['like_count']]
X2_test = bowen.test1.drop(['forward_count', 'comment_count', 'like_count'], axis=1)

model_xgb2 = xgb.XGBRegressor(max_depth=4, colsample_btree=0.1, learning_rate=0.1, n_estimators=320, min_child_weight=2)
model_xgb2.fit(X2_train, y2_train) #训练模型
joblib.dump(model_xgb2,'''dz_model.m''') #保存模型
xgb_pred2 = model_xgb2.predict(X2_test)
print(xgb_pred2)

xgb_pred_new2=[]
for i in xgb_pred2:
    if i<0:
        i=0
    i=int(i)
    xgb_pred_new2.append(i)
xgb_pred_new2=pd.DataFrame(xgb_pred_new2)


# print('mse:', mean_squared_error(y_test, xgb_pred))     #计算均方误差回归损失
#准确率比较
print('转发正确率:', bowen.accuracy_score(y_test,xgb_pred_new))
print('评论正确率:', bowen.accuracy_score(y1_test,xgb_pred_new1))
print('赞正确率:', bowen.accuracy_score(y2_test,xgb_pred_new2))

